package com.centerm.demo;

import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.centerm.demo.jf.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by linwenhui on 2017/5/2.
 */

public class BluetoothDialog extends Dialog implements DialogInterface.OnKeyListener {
    private ListView listView;
    private BluetoothAdapter adapter;

    public BluetoothDialog(Context context) {
        super(context, R.style.DialogStyle);
        setContentView(R.layout.dialog_bluetooth);
        setCancelable(false);
        listView = (ListView) findViewById(R.id.lstvw);
        adapter = new BluetoothAdapter(context);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(adapter);
        setOnKeyListener(this);
    }

    public void addAll(List<BluetoothDevice> devices) {
        adapter.addAll(devices);
    }

    public void add(BluetoothDevice device){
        adapter.add(device);
    }

    public BluetoothDevice getSel() {
        return adapter.getSel();
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return false;
    }


    private class BluetoothAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

        private List<BluetoothDevice> devices;
        private LayoutInflater inflater;
        private int sel = -1;

        public BluetoothAdapter(Context context) {
            this.devices = new ArrayList<>();
            inflater = LayoutInflater.from(context);
        }

        public void addAll(List<BluetoothDevice> devices) {
            this.devices.addAll(devices);
            notifyDataSetChanged();
        }

        public void add(BluetoothDevice device){
            this.devices.add(device);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return devices.size();
        }

        public BluetoothDevice getSel() {
            if (sel != -1)
                return getItem(sel);
            return null;
        }

        @Override
        public BluetoothDevice getItem(int position) {
            return devices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            TextView txtvw = (TextView) convertView;
            txtvw.setTextColor(Color.BLACK);
            BluetoothDevice device = getItem(position);
            txtvw.setText("蓝牙名：" + device.getName() + "\n地址：" + device.getAddress());
            return convertView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            sel = position;
            dismiss();
        }
    }
}
